#!/bin/bash

E_MC_INLCUDE_ARGSLIB=65
E_MC_INLCUDE_ARGSLIB_MSG="Impossibile includere argslib.sh"
E_MC_INLCUDE_LOGLIB=66
E_MC_INLCUDE_LOGLIB_MSG="Impossibile includere loglib.sh"

# include argslib.sh
. ../bash/argslib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_ARGSLIB_MSG && exit $E_MC_INLCUDE_ARGSLIB

# include loglib.sh
. ../bash/loglib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_LOGLIB_MSG && exit $E_MC_INLCUDE_LOGLIB

P_HOSTNAME=localhost
P_PORT=8003
P_CODEBASE_PATH=file://$HOME/public_html/common/
P_POLICY=./policy
P_LOG_RMID_PATH=$HOME/PAR/log-rmid

G_LOGPOS=$(basename $0)

OPTIONS=( --hostname --codebase-path --policy-file --log-rmid-path --stop --help )
HELPS=( "hostname" "codebase path" "policy file path" "log rmid path" "spegne il server rmid" "prints help page" )
DEFAULTS=( "$P_HOSTNAME" "$P_CODEBASE_PATH" "$P_POLICY" "$P_LOG_RMID_PATH" "" "" )

function stop_rmid()
{
	echo 
	rmid -stop &> /dev/null
	killall -9 rmid &> /dev/null
}


for i in $@; do
	case $i in 
	--hostname=*)
		argslib_get_long_option $i
		shift
		P_HOSTNAME="$ARGSLIB_OPT_VALUE"
		;;
	--codebase-path=*)
		argslib_get_long_option $i
		shift
		P_CODEBASE_PATH="$ARGSLIB_OPT_VALUE"
		;;
	--policy-file=*)
		argslib_get_long_option $i
		shift
		P_POLICY="$ARGSLIB_OPT_VALUE"
		;;
	--log-rmid-path=*)
		argslib_get_long_option $i
		shift
		P_LOG_RMID_PATH="$ARGSLIB_OPT_VALUE"
		;;
	--stop)
		shift
		stop_rmid 
		exit 0
		;;
	--help)
		argslib_usage
		shift
		exit 0
		;;
	*)
		shift
		;;
	esac
done

bt_info "stop_rmid" "stop any active rmid server" $G_LOGPOS 
bt_info "rm -rf $P_LOG_RMID_PATH/*" "clear rmid log dir" $G_LOGPOS $BT_OUT_TYPE_NOOUT

sleep 1

bt_info_inline "run the rmid server" $G_LOGPOS
rmid -log "$P_LOG_RMID_PATH"/ \
-J-Djava.rmi.server.codebase=$P_CODEBASE_PATH \
-J-Djava.rmi.server.hostname=$P_HOSTNAME \
-J-Djava.rmi.dgc.leaseValue=30000 \
-J-Djava.security.policy=$P_POLICY & 

